package org.dtrd.modules.shop.service.impl;

import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninPlan;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdProductPush;
import org.dtrd.modules.shop.mapper.DtrdEntShcRdProductPushMapper;
import org.dtrd.modules.shop.service.IDtrdEntShcRdProductPushService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.shop.entity.bean.ProductPushDetail;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 商品推送表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-09-03
 */
@DS("multi-datasource1")
@Service
public class DtrdEntShcRdProductPushServiceImpl extends ServiceImpl<DtrdEntShcRdProductPushMapper, DtrdEntShcRdProductPush> implements IDtrdEntShcRdProductPushService {

    private DtrdEntShcRdProductPushMapper dtrdEntShcRdProductPushMapper;

    @Override
    public ProductPushDetail getProductPush(Integer dataId) {
        DtrdEntShcRdProductPush dtrdEntShcRdProductPush = getById(dataId);
        return Optional.ofNullable(dtrdEntShcRdProductPush)
                        .map(po -> new ProductPushDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateProductPush(ProductPushDetail dto) {
        DtrdEntShcRdProductPush dtrdEntShcRdProductPush = new DtrdEntShcRdProductPush();
        if(dto.getDataId() == null){
            dtrdEntShcRdProductPush = dtrdEntShcRdProductPush.init();
        }
        dtrdEntShcRdProductPush.parseFromDto(dto);
        return saveOrUpdate(dtrdEntShcRdProductPush);
    }

    @Override
    public boolean removeProductPush(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public ProductPushDetail getLastPushProduct(Integer patientId) {
        LambdaQueryWrapper<DtrdEntShcRdProductPush> wrapper = Wrappers.lambdaQuery(DtrdEntShcRdProductPush.class)
                .eq(DtrdEntShcRdProductPush::getPatientId, patientId)
                .orderByDesc(DtrdEntShcRdProductPush::getCreateTime)
                .last("limit 1");
        DtrdEntShcRdProductPush push = getOne(wrapper);
        return Optional.ofNullable(push)
                .map(po -> new ProductPushDetail().parseFromPo(po))
                .orElse(null);
    }

    private LambdaQueryWrapper<DtrdEntShcRdProductPush> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntShcRdProductPush.class)
                        .eq(DtrdEntShcRdProductPush::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntShcRdProductPush> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntShcRdProductPush.class)
                        .eq(DtrdEntShcRdProductPush::getIsDel, 0);
    }
}
